package cn.ms.springbootimagedemo.Utils;

import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

@Component
public class ImageUtils {


    /**
     * 获取爬图片
     * @param response
     * @param qq
     */
    public void renderImage(HttpServletResponse response, Long qq) {
        // Set to expire far in the past.
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
        BufferedImage bufferedImage = joinPortraitAndPa(qq);

        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ImageIO.write(bufferedImage, "jpg", out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }




    private BufferedImage joinPortraitAndPa(Long qq){
        BufferedImage paImage = getPaImage();
        BufferedImage qqPortrait = getQQPortrait(qq);
        int paWidth=paImage.getWidth();
        int paHeight=paImage.getHeight();
        Graphics2D graphics = paImage.createGraphics();
        graphics.drawImage(qqPortrait,20,paHeight-paWidth/6-10,paWidth/6,paHeight/6,null);
        graphics.dispose();
        return paImage;
    }

    /**
     * 获取爬的图片
     * @return
     */
    private BufferedImage getPaImage(){
        int imageNum= (int) (Math.random()*46)+1;
        System.out.println(imageNum);
        String path=this.getClass().getClassLoader().getResource("").getPath()+"pa/"+imageNum+".jpg";
        System.out.println();
        BufferedImage read=null;
        try {
            read = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return read;
    }
    /**
     * 获取QQ头像
     * @param qq
     * @return
     */
    private BufferedImage getQQPortrait(Long qq){
        String api="http://q1.qlogo.cn/g?b=qq&nk="+qq+"&s=640";
        BufferedImage image=null;
        try {
            URL url=new URL(api);
            image = ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int paWidth=image.getWidth();
        int paHeight=image.getHeight();
        // 透明底的图片
        BufferedImage formatAvatarImage = new BufferedImage(paWidth, paHeight, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = formatAvatarImage.createGraphics();
        //把图片切成一个园
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //留一个像素的空白区域，这个很重要，画圆的时候把这个覆盖
        int border = 1;
        //图片是一个圆型
        Ellipse2D.Double shape = new Ellipse2D.Double(border, border, paWidth - border * 2, paHeight - border * 2);
        //需要保留的区域
        graphics.setClip(shape);
        graphics.drawImage(image, border, border, paWidth - border * 2, paHeight - border * 2, null);
        graphics.dispose();
        //在圆图外面再画一个圆
        //新创建一个graphics，这样画的圆不会有锯齿
        graphics = formatAvatarImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int border1 = 3;
        //画笔是4.5个像素，BasicStroke的使用可以查看下面的参考文档
        //使画笔时基本会像外延伸一定像素，具体可以自己使用的时候测试
        Stroke s = new BasicStroke(5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        graphics.setStroke(s);
        graphics.setColor(Color.white);
        graphics.drawOval(border1, border1, paWidth - border1 * 2, paHeight - border1 * 2);
        graphics.dispose();
        return formatAvatarImage;
    }
}
